import axios from 'axios'
import BASE_URL from './base-url';

declare var vscode: any;
const getHeader = (key: string) => {
  const data = vscode.getState() || {}
  return data[key]
}

axios.defaults.baseURL = `${BASE_URL}/program-center`
// axios.defaults.baseURL = 'http://localhost:8090/'
axios.defaults.headers.common['Access-Control-Allow-Origin'] = '*'
axios.defaults.headers.common['Content-Type'] = 'application/json;charset=utf-8'


export const get = async (url: string) => {
  try {
    const res = await axios.get(url, {
      headers: {
        token: getHeader('jwt'),
        sessionId: getHeader('sessionId'),
        id: getHeader('userId') || '5'
      }
    })
    return res.data
  } catch (e) {
    console.error(e.message);
  }
}
export const del = async (url: string) => {

  try {
    await axios.delete(url, {
      headers: {
        token: getHeader('jwt'),
        sessionId: getHeader('sessionId'),
        id: getHeader('userId') || '5'
      }
    })
  } catch (e) {
    console.error(e.message);
  }
}
export const post = async (url: string, data: any): Promise<any> => {
  try {
    const res = await axios.post(url, data, {
      headers: {
        token: getHeader('jwt'),
        sessionId: getHeader('sessionId'),
        id: getHeader('userId') || '5'
      }
    })
    return res
  } catch (e) {
    console.error(e.message);
  }
}
export const update = async (url: string, data: any, ) => {

  try {
    await axios.patch(url, data, {
      headers: {
        token: getHeader('jwt'),
        sessionId: getHeader('sessionId'),
        id: getHeader('userId') || '5'
      }
    })
  } catch (e) {
    console.error(e.message);
  }
}

